package com.ruge._08_springboot_springdata_jpa.controller._03_mapping._01_oneToOne;

import com.ruge._08_springboot_springdata_jpa.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2019-07-05  22:43
 * 描述 ：一对一测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)//这里是启动类
public class TestOneToOne {
    @Autowired
    private EntityManager entityManager;

    @Test
    @Transactional
    public void testSave() {
        OneToOneClassModel classModel = new OneToOneClassModel();
        OneToOneMonitorModel monitorModel = new OneToOneMonitorModel();
        classModel.setName("三年二班");
        monitorModel.setName("周杰伦");
        classModel.setOneToOneMonitorModel(monitorModel);
        entityManager.persist(classModel);
        entityManager.persist(monitorModel);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testUpdate() {
        OneToOneClassModel classModel =   entityManager.find(OneToOneClassModel.class,5);
        OneToOneMonitorModel monitorModel =   entityManager.find(OneToOneMonitorModel.class,6);
        classModel.setName("三年三班");
        monitorModel.setName("嘿丷如歌");
        classModel.setOneToOneMonitorModel(monitorModel);
        entityManager.merge(classModel);
        entityManager.merge(monitorModel);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testDelete() {
        OneToOneClassModel classModel =   entityManager.find(OneToOneClassModel.class,5);
        entityManager.remove(classModel);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testSelect() {
        List list = entityManager.createQuery("select  t from OneToOneClassModel t").getResultList();
        list.forEach(System.out::println);
    }
}
